package com.atguigu.gmall.all.controller;

/**
 * @author Sanmier
 * @create 2020-12-16 20:52
 */

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 购物车页面
 * </p>
 *
 */
@Controller
public class CartController {

    @Autowired
    private CartFeignClient cartFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;


    // 通过用户Id 查询购物车列表
    @RequestMapping("cart.html")
    public  String cartList(){
        //返回页面  需要存储数据吗 ？不需要  因为异步调用 “api/cart/cartList”
        return "cart/index";
    }


    // 添加购物车
    @RequestMapping("addCart.html")
    public String addCart(HttpServletRequest request){
        //根据skuId查询skuInfo

        String skuId = request.getParameter("skuId");
        String skuNum = request.getParameter("skuNum");

        SkuInfo skuInfo = productFeignClient.getSkuInfo(Long.parseLong(skuId));

        cartFeignClient.addToCart(Long.parseLong(skuId), Integer.parseInt(skuNum));
        //需要存储skuInfo
        request.setAttribute("skuInfo", skuInfo);
        // 存储skuNum
        request.setAttribute("skuNum", skuNum);
        //返回页面
        return "cart/addCart";
    }


}
